<?php

namespace app\api\model;

use app\common\model as M;
use traits\ModelTrait;
use basic\ModelBasic;

/**
 * 用户通知 model
 * Class UserNotice
 * @package app\admin\model\user
 */
class Course extends ModelBasic
{
    use ModelTrait;

    /**
     * 获取课程大纲安排
     * @Author   FW
     * @DateTime 2021-05-17
     * @Email    123763712@qq.com
     * @param    array            $where [description]
     * @return   [type]                  [description]
     */
    public static function my_course_outline($where = [] ){
        $sql = '1=1';
        if ( isset($where['parent_uid']) ) {
            $sql .= sprintf(' AND outline.course_id IN (SELECT DISTINCT course_id FROM `eb_school_enroll` WHERE parent_uid=%d AND org_id=%d AND status=1)' , $where['parent_uid'] , $where['org_id']);
        }

        if ( isset($where['course_day']) ) {
            $sql .= sprintf(" AND outline.course_day='%s'" , $where['course_day']);
        }

        /** 已完结课程 */
        if ( isset($where['complete']) && ($where['complete'] == true) ) {
            $sql .= sprintf(" AND outline.course_end <'%s'" , date('Y-m-d'));
        }

        $sql = sprintf("
            SELECT
                outline.outline_title,outline.start_time,outline.end_time,outline.course_day,outline.class_id,outline.course_id
                ,cour.title as course_name
                ,sca.title_label,sca.shot_desc,sca.course_start,sca.course_end,sca.time_quantum,sca.total_period_classes,sca.left_period_classes
                ,sch.school_name
                ,t.fullname,t.class_name
            FROM `eb_school_class_outline` AS outline
            LEFT JOIN (
                SELECT cls.id as class_id,cls.class_name,t.fullname
                FROM `eb_school_class` `cls`
                LEFT JOIN `eb_teacher_info` `t` ON `cls`.`teacher_uid` = `t`.`teacher_uid`
                GROUP BY class_id
            ) AS t ON outline.class_id = t.class_id

            LEFT JOIN `eb_school_course` AS cour ON outline.course_id = cour.id
            LEFT JOIN `eb_school` AS sch ON cour.school_id = sch.id
            LEFT JOIN `eb_school_class_attr` AS sca ON outline.course_id = sca.course_id AND outline.class_id = sca.class_id
            WHERE %s
            GROUP BY class_id,course_id,course_day
        " , $sql);
        $outlines = self::query($sql);

        if ( !empty($outlines) ) {
            foreach ( $outlines as &$outline ) {
                $outline['start_time'] = substr($outline['start_time'], 0, 5);
                $outline['end_time']   = substr($outline['end_time'], 0, 5);
                $outline['date_show']  = date('m月d日' , strtotime($outline['course_start'])) . '-' . date('m月d日' , strtotime($outline['course_end'])) . ' ' . $outline['time_quantum'];
            }
        }
        return $outlines;
    }

    /**
     * 获取我的课程
     * @Author   FW
     * @DateTime 2021-05-24
     * @Email    123763712@qq.com
     * @param    array            $where [description]
     * @return   [type]                  [description]
     */
    public static function my_courses($where = [] ){
        // $model = $model===null ? new self() : $model;

        // $alias = 'cour';
        // $model->alias($alias);

        // $org_id = static::getBelongOrganId();
        // if ( $org_id > 0 ) {
        //     $model->where($alias.'.id',$org_id);
        // }

        // if ( isset($where['parent_uid']) && $where['parent_uid'] > 0 ) {
        //     $sql = sprintf('cour.id IN (SELECT DISTINCT course_id FROM `eb_school_enroll` WHERE parent_uid=%d)' , $where['parent_uid']);
        //     $model->where($sql);
        // }


        // $count = $model->count();
        // $data  = [];
        // if ( $count == 0 ) {
        //     return compact('data','count');
        // }

        // $data=self::setWhere($where,'m')
        // ->field('cour.title as course_name
        //         ,sca.title_label,sca.shot_desc,sca.course_start,sca.course_end,sca.time_quantum,sca.total_period_classes,sca.left_period_classes
        //         ,sch.school_name
        //         ,t.fullname
        //         ,enroll.course_id,enroll.class_id,enroll.stu_id,enroll.stu_name')
        // ->join('__SCHOOL_COURSE_ATTR__ attr','attr.course_id = cour.id' , 'left')
        // ->join('__SCHOOL_ENROLL__ enroll','enroll.course_id = cour.id' , 'left')
        // ->join('__SCHOOL_CLASS_ATTR__ sca','enroll.course_id = sca.course_id AND enroll.class_id = sca.class_id' , 'left')
        // ->join('__SCHOOL__ sch','cour.school_id = sch.id' , 'left')
        // ->join('__SCHOOL_CLASS__ cls','enroll.class_id = cls.id' , 'left')
        // ->join('__TEACHER_INFO__ cls','enroll.class_id = cls.id' , 'left')
        // ->page((int)$where['page'],(int)$where['limit'])
        // ->select();

        // if ( !empty($data) ) {
        //     $data = $data->toArray();
        //     $data = static::process_list($data);
        // }

        // return compact('data','count');


        $sql = '1=1';
        if ( isset($where['parent_uid']) ) {
            $sql .= sprintf(' AND enroll.parent_uid=%d AND enroll.status=1 AND enroll.org_id=%d',$where['parent_uid'] ,$where['org_id']);
        }

        /** 已完结课程 */
        if ( isset($where['complete']) && ($where['complete'] == true) ) {
            $sql .= sprintf(" AND enroll.course_end <'%s'" , date('Y-m-d'));
        }

        $count  = M\SchoolEnroll::alias('enroll')->where($sql)->count();
        $page   = $where['page'] ?? 1;
        $limit  = $where['limit'] ?? 10;
        $offset = ($page - 1) * $limit;
        $sql = sprintf("
            SELECT
                cour.title as course_name
                ,sca.title_label,sca.shot_desc,sca.course_start,sca.course_end,sca.time_quantum,sca.total_period_classes,sca.left_period_classes
                ,sch.school_name
                ,t.fullname,t.class_name,t.avatar
                ,enroll.course_id,enroll.class_id,enroll.stu_id,enroll.stu_name,(enroll.buy_nums+enroll.give_nums) as total_nums,enroll.left_nums,enroll.nums_unit,enroll.status,enroll.pay_status
            FROM `eb_school_enroll` AS enroll
            LEFT JOIN `eb_school_class_attr` AS sca ON enroll.course_id = sca.course_id AND enroll.class_id = sca.class_id
            LEFT JOIN `eb_school_course_attr` AS attr ON attr.course_id = enroll.id
            LEFT JOIN `eb_school_course` AS cour ON enroll.course_id = cour.id
            LEFT JOIN `eb_school` AS sch ON enroll.school_id = sch.id
             LEFT JOIN (
                SELECT cls.id as class_id,cls.class_name,t.avatar,t.fullname
                FROM `eb_school_class` `cls`
                LEFT JOIN `eb_teacher_info` `t` ON `cls`.`teacher_uid` = `t`.`teacher_uid`
                GROUP BY class_id
            ) AS t ON enroll.class_id = t.class_id
            WHERE %s
            -- GROUP BY enroll.class_id,enroll.course_id
            LIMIT %d,%d
        " , $sql , $offset , $limit);
        $data = self::query($sql);

        if ( !empty($data) ) {
            foreach ( $data as &$v ) {
                $v['date_show']  = date('m月d日' , strtotime($v['course_start'])) . '-' . date('m月d日' , strtotime($v['course_end'])) . ' ' . $v['time_quantum'];
                $v['nums_unit_display'] = M\SchoolClassAttr::$units[$v['nums_unit']] ?? $v['nums_unit'];
                $v['status_display']    = M\SchoolEnroll::$status[$v['status']] ?? '未知';
                $v['pay_status_display']    = M\SchoolEnroll::$pay_status[$v['pay_status']] ?? '未知';
                $v['course_end']        = date('Y年m月d日' , strtotime($v['course_end']));

                if ( isset($v['avatar']) ) {
                    $v['avatar'] = json_decode($v['avatar'] , true);
                }
            }
        }
        return compact('count' , 'data');
    }
} //Class End